// 获取文件后缀名
export function getFileExt (fileName)
{
    return fileName.slice(fileName.lastIndexOf('.') + 1);
}

// 数组去重 
export function unique (arr)
{
    return Array.from(new Set(arr));
}

// 哈希256加密  
export function hash256 (str)
{
    let hash = 0, i, chr;
    if (str.length === 0) return hash;
    for (i = 0; i < str.length; i++)
    {
        chr = str.charCodeAt(i);
        hash = ((hash << 5) - hash) + chr;
        hash |= 0; // Convert to 32bit integer
    }
    return hash;
}

// 判断当前浏览器版本是否支持webp
export function supportWebp ()
{
    return (
        window.WebP &&
        document.createElement('canvas').toDataURL('image/webp').indexOf('data:image/webp') === 0
    );
}

// 判断当前浏览器版本信息   
export function browser ()
{
    let ua = navigator.userAgent.toLowerCase();
    let match = /(chrome)[ \/]([\w.]+)/.exec(ua) ||
        /(webkit)[ \/]([\w.]+)/.exec(ua) ||
        /(opera)(?:.*version|)[ \/]([\w.]+)/.exec(ua) ||
        /(msie) ([\w.]+)/.exec(ua) ||
        ua.indexOf('compatible') < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua) ||
        [];
    return {
        browser:match[1] || '',
        version:match[2] || '0'
    };
}

// 判断是否是移动端环境 
export function isMobile ()
{
    let u = navigator.userAgent;
    let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
    let isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    return isAndroid || isiOS;
}

// 正则匹配手机号码
export function isPhone (phone)
{
    return /^1[3456789]\d{9}$/.test(phone);
}

// 正则匹配qq号码
export function isQQ (qq)
{
    return /^[1-9][0-9]{4,}$/.test(qq);
}

// 判断是否是空对象 
export function isEmptyObject (obj)
{
    for (let key in obj)
    {
        return false;
    }
    return true;
}

// 过滤对象为空的属性   
export function filterEmpty (obj)   // 参数为对象
{
    let newObj = {};
    for (let key in obj)
    {
        if (obj[key] !== '')
        {
            newObj[key] = obj[key];
        }
    }
    return newObj;
}

// 判断是否是对象   
export function isObject (obj)
{
    return Object.prototype.toString.call(obj) === '[object Object]';
}

// 数据监视器
export function watch (obj, callback)
{
    let newObj = {};
    for (let key in obj)
    {
        newObj[key] = obj[key];
    }
    let oldObj = {};
    for (let key in obj)
    {
        oldObj[key] = obj[key];
    }
    setInterval(() => 
    {
        for (let key in obj)
        {
            if (obj[key] !== oldObj[key])
            {
                oldObj[key] = obj[key];
                callback(key, obj[key]);
            }
        }
    }, 100);
}

